package com.juyu.miliao.common.pojo;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.Range;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Page<T> implements Serializable{
	private static final long serialVersionUID = -6304896522588369906L;
	@Min(1)
	private int page = 1;
	@Range(min = 1, max = 20)
	private int pageSize = 15;
	private int totalPages = 0;
	private long totalRecords = 0;
	private List<T> list = Collections.emptyList();
 
	public Page() {

	}

	public Page(int page, int pageSize) {
		this.page = page;
		this.pageSize = pageSize;
	}
	
	@JsonIgnore
	public int getPage() {
		return page;
	}

	public Page<T> setPage(int page) {
		this.page = page;
		return this;
	}

	@JsonIgnore
	public int getPageSize() {
		return pageSize;
	}

	public Page<T> setPageSize(int pageSize) {
		this.pageSize = pageSize;
		return this;
	}

	@JsonIgnore
	public int getOffset() {
		return (page - 1) * pageSize;
	}
	
	@JsonIgnore
	public int getEndset(int record) {
	if (pageSize*page > record) {
		return record;
		}
		return getOffset() + pageSize;
	}

	public Page<T> setTotalRecords(long totalRecords) {
		this.totalRecords = totalRecords;
		this.totalPages = (int) Math.ceil((double) totalRecords / (double) pageSize);
		return this;
	}
	
	public long getTotalRecords() {
		return totalRecords;
	}

	public Page<T> setList(List<T> list) {
		if(list != null)
			this.list = list;
		return this;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public List<T> getList() {
		return list;
	}
	
}
